#region Copyright RenGuiYou. All rights reserved.
//=====================================================
// PandoraFrameWork
// Author:      RenGuiyou
// Feedback: 	mailto:750539605@qq.com
//=====================================================
#endregion
using System;
using Neatly;
using Pandora;
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;

namespace PandoraEditor.Window
{
    public class GlobalExpress : NavFuncBase<Object>
    {
        private const string CHEKC_COMPONENT = "Component检测";
        private const string CHEKC_SCRIPT_MISS = "脚本丢失";
        
        private const string CHEKC_SPRITE_NAME = "SpriteName检测";
        private const string SPRITE_NAME_REPEAT = "Sprite名字重复";
        private const string SPRITE_NAME_HAS_EMPTY = "Sprite名字错误";
        
        private const string CHEKC_FILE_NAME = "文件名检测";
        private const string CHEKC_FILE_ERROR = "文件名错误";
        public GlobalExpress()
        {
            AddNavPrefabAction(CHEKC_COMPONENT, CheckComponentMiss);
            AddNavSpriteAction(CHEKC_SPRITE_NAME, CheckSpriteName);
            AddNavFileAction(CHEKC_FILE_NAME);
        }

        #region Navigation

        private AssetObject _cacheObject;
        private void CheckComponentMiss(AssetObject handlers)
        {
            _cacheObject = handlers;
            RecursionCheckComponent(handlers.gameObject);
        }

        private void CheckSpriteName(AssetObject handlers)
        {
            var title = SPRITE_NAME_REPEAT;
            var sprite = handlers.sprite;
            if (sprite.name.IndexOf(' ') != -1 || sprite.name.IndexOf(".", StringComparison.Ordinal) != -1)
            {
                title = SPRITE_NAME_HAS_EMPTY;
            }
            m_ResultSet.Add(CHEKC_SPRITE_NAME, title, handlers.sprite);
            LogAppend(title, handlers.sprite);
        }
        #endregion

        private void AddNavFileAction(string title)
        {
            EditorUtility.DisplayProgressBar(title, "Please Wait......", 0);
            PdrWindowHelper.ClearConsole();
            AddNavButton(title, () =>
            {
                Clear();
                var fileList = PdrFileUtil.GetFilesName("assets");
                for (int i = 0; i < fileList.Count; i++)
                {
                    if (fileList[i].Contains(" "))
                    {
                        Debug.Log(fileList[i]);
                    }
                }
                Debug.Log(fileList.Count);
                WriteLog();
            });
            EditorUtility.ClearProgressBar();
        }
        
        private void AddNavPrefabAction(string title, params Action<AssetObject>[] handlers)
        {
            AddNavButton(title, () =>
            {
                Clear();
                string str = "Check All Prefabs";
                PdrWindowHelper.CheckAllPrefabs(str, handlers);
                WriteLog();
            });
        }

        private void AddNavSpriteAction(string title, params Action<AssetObject>[] handlers)
        {
            AddNavButton(title, () =>
            {
                Clear();
                string str = "Check All Sprite";
                PdrWindowHelper.CheckUISpritesName(str, handlers);
                WriteLog();
            });
        }

        #region Logic
        private void RecursionCheckComponent(GameObject go)
        {
            foreach (Transform son in go.transform)
            {
                RecursionCheckComponent(son.gameObject);
            }
            Component[] components = go.GetComponents<Component>();
            foreach (var c in components)
            {
                if (c == null)
                {
                    m_ResultSet.Add(CHEKC_COMPONENT, CHEKC_SCRIPT_MISS, _cacheObject.gameObject);
                    LogAppend(CHEKC_SCRIPT_MISS, _cacheObject, go);
                }
            }
        }
        #endregion
    }
}